using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Collections;
using System.Data;
using System.Data.OleDb;
namespace DAOLayer
{
    public class TuaSachDAO:AbStractDao
    {
        public TuaSachDAO()
        {
            this.connect();
        }
       
        public DataTable getListTuaSach()
        {
            string query = "SELECT * FROM TuaSach";
            TuaSachDAO dgDao = new TuaSachDAO();
            DataTable dt = dgDao.fillDataTable(query);
            dgDao.disconnect();           
            return dt;
        }
        public TuaSachDTO getTuaSach(int maTuaSach)
        {
            string strQuery = "select * from TuaSach where MaTuaSach= " + maTuaSach;
            TuaSachDAO dgDao = new TuaSachDAO();
            IDataReader dr = dgDao.executeQuery(strQuery);
            TuaSachDTO dgDto = new TuaSachDTO();
            if (dr.Read())
            {
                dgDto.MaTuaSach = dr.GetInt32(0);
                dgDto.TuaSach = dr.GetString(1);
                dgDto.TacGia = dr.GetString(2);
                dgDto.TomTat = dr.GetString(3);              
            }
            dgDao.disconnect();
            return dgDto;
        }
        public bool insert(ref TuaSachDTO info)
        {

            string insertCommand = "INSERT INTO TuaSach(tenTuaSach,tacGia,tomTat) VALUES('" +
                info.TuaSach + "', '" +
                info.TacGia + "', '" +
                info.TomTat + "')";
            bool flag = executeNonQuery(insertCommand);     
            return flag;
        }
        public bool update(TuaSachDTO info)
        {
            string updateCommand = "UPDATE TuaSach " +
                                    "SET tenTuaSach = '"+info.TuaSach+"',"+
                                    "tacGia = '"+info.TacGia+"',"+
                                    "tomTat = '" + info.TomTat +  "'" +                                
                                    " WHERE maTuaSach = " + info.MaTuaSach ;
            bool flag = this.executeNonQuery(updateCommand);
    
            return flag;
        }
        public int LayMaTuaSach(string tensach)
        {
            int kq=0;
            string strSQL = "select maTuaSach from TuaSach where tenTuaSach= '" + tensach + "'";
            IDataReader dr= this.executeQuery(strSQL);
            if (dr.Read())
                kq = int.Parse(dr[0].ToString().Trim());
            return kq;
        }
        public bool delete(int maTuaSach)
        {
            DauSachDAO dsDao = new DauSachDAO();
            if (dsDao.kiemTraTonTai(maTuaSach))
                return false;
            string deleteCommand = "DELETE FROM TuaSach WHERE maTuaSach = " + maTuaSach;
            bool flag = this.executeNonQuery(deleteCommand);
            return flag;
        }
        public TuaSachDTO LoadThongTinTuaSach(string TuaSach)
        {
            TuaSachDAO tuasachDAO = new TuaSachDAO();
            int matuasach =tuasachDAO.LayMaTuaSach(TuaSach);
            TuaSachDTO tuasachDTO = new TuaSachDTO();
            tuasachDTO = tuasachDAO.getTuaSach(matuasach);
            return tuasachDTO;
        }
        public DataTable getlistTuaSach()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        
    }
}
